from flask import Flask
import json
import time
from concurrent.futures import ThreadPoolExecutor

app = Flask(__name__)

def read_file():
    time.sleep(0.1)
    return 'file result'


def read_db():
    time.sleep(0.2)
    return 'db result'


def read_api():
    time.sleep(0.3)
    return 'api result'

# 600多毫秒
@app.route('/')
def index():
    file = read_file()
    db = read_db()
    api = read_api()
    return json.dumps({'file': file, 'db': db, 'api': api})

#改造, 300多毫秒
@app.route('/abc')
def abc():
    file = pool.submit(read_file)
    db = pool.submit(read_db)
    api = pool.submit(read_api)
    return json.dumps({'file': file.result(), 'db': db.result(), 'api': api.result()})


if __name__ == '__main__':
    pool = ThreadPoolExecutor()
    app.run()